package com.lgt_enterprise_android.view.pages;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.adapter.MyStaffUnJoinAdapter;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.HttpManager;
import com.swipemenulistview.SwipeMenu;
import com.swipemenulistview.SwipeMenuCreator;
import com.swipemenulistview.SwipeMenuItem;
import com.swipemenulistview.SwipeMenuListView;
import com.util.AppUtil;

public class InviteRecordActivity extends BaseActivity {

    private SwipeMenuListView mSwipeMenuListView;
    private MyStaffUnJoinAdapter adapter;

    private JSONArray mJsonArray = new JSONArray();

    private int pageIdx = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_record);
        initView();
        getData();
    }


    private void initView() {
        mSwipeMenuListView = (SwipeMenuListView) findViewById(R.id.swipeMenuListView);
        mSwipeMenuListView.onLoadComplete();

        mSwipeMenuListView.setOnRefreshListener(new SwipeMenuListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageIdx = 1;
                getData();
            }
        });


        mSwipeMenuListView.setOnLoadListener(new SwipeMenuListView.OnLoadListener() {
            @Override
            public void onLoad() {
                pageIdx++;
                getData();
                mSwipeMenuListView.onLoadComplete();
            }
        });

        mSwipeMenuListView.isShowIsload(false);
        mSwipeMenuListView.setMenuCreator(new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                if (menu.getViewType() == 0) {
                    SwipeMenuItem item = new SwipeMenuItem(mContext);
                    item.setBackground(new ColorDrawable(Color.rgb(0xf4, 0x50, 0x2c)));
                    item.setWidth(110 * AppUtil.getdensityIntDpi(mContext));
                    item.setTitle("再次邀请");
                    item.setTitleSize(12);
                    item.setTitleColor(Color.WHITE);
                    menu.addMenuItem(item);
                } else {

                }
            }
        });

        mSwipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                JSONObject jo = ((JSONObject) adapter.getItem(position));
                JSONObject tmp = new JSONObject();
                tmp.put("name", jo.getString("name"));
                tmp.put("phoneNumber", jo.getString("phoneNumber"));

                JSONArray jsonArray = new JSONArray();
                jsonArray.add(tmp);

                HttpManager.inviteStaff(mContext, jsonArray, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        showToast("邀请成功");
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });
            }
        });


        adapter = new MyStaffUnJoinAdapter(mContext, mJsonArray);
        mSwipeMenuListView.setAdapter(adapter);
    }

    private void getData() {
        HttpManager.getStaffListUnJoin(mContext, pageIdx, new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(Object resonseObj) {
                JSONArray jsonArray = ((JSONObject) resonseObj).getJSONArray("list");
                if (pageIdx == 1) {
                    adapter.setData(jsonArray);
                    mSwipeMenuListView.onRefreshComplete();
                } else {
                    adapter.addData(jsonArray);
                    mSwipeMenuListView.onLoadComplete();
                }
                mSwipeMenuListView.setResultSize(jsonArray.size());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String mes) {

            }
        });

    }
}
